package com.mrjy.weixin.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.WeixinMenu;
import com.mrjy.common.entity.WeixinMenuGroup;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.weixin.bo.IWeixinMenuBo;
import com.mrjy.weixin.util.CopyList;

@Controller
@RequestMapping("/weixin/weixinMenu")
public class WeixinMenuController  {
	@Resource
	private IWeixinMenuBo weixinMenuBo;
	
	/**
	 * 显示list
	 * @return
	 */
	@RequestMapping("/weixinMenuGroupList")
	@ResponseBody
	public Object weixinMenuGroupList(WeixinMenuGroup weixinMenuGroup,HttpSession session) {
		UserInfo userInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		if(userInfo==null){
			return null;
		}else{
			if(userInfo.getSchoolId()!=null){
				weixinMenuGroup.setSchoolId(userInfo.getSchoolId());
			}else{
				weixinMenuGroup.setSchoolId(1);
			}
		}
		return weixinMenuBo.weixinMenuGroupList(weixinMenuGroup);
	}
	
	/**
	 * 根据ID查询
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryById")
	@ResponseBody
	public Object queryById(int id) {
		return weixinMenuBo.queryById(id);
	}
	
	/**
	 * DB查询菜单树
	 * @param id
	 * @return
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	@RequestMapping("/queryMenuTree")
	@ResponseBody
	public Object queryMenuTree(int id) {
		return weixinMenuBo.queryMenuTree(id);
	}
	
	/**
	 * DB查询菜单树
	 * @param id
	 * @return
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	@RequestMapping("/queryMenuTreeByS")
	@ResponseBody
	public Object queryMenuTreeByS(String appId,String appAccount,String ihost,String appDomain, HttpSession session,HttpServletRequest request ) throws ClassNotFoundException, IOException {
		List<WeixinMenu> list = new ArrayList<WeixinMenu>();
		if(session.getAttribute("weixin_menu_group")==null){
			String guanwangurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri=http%3A%2F%2F"+ihost+"%2FphoneWeb%2Fweixin_index.html%3Fappc%3D"+appAccount+"%26orgsc%3D"+appDomain+"%26turl%3Dhttp%3A%2F%2F"+ihost+"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirectWxApiClient";
			String bindingurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri=http%3A%2F%2F"+ihost+"%2FphoneWeb%2Fbinding_index.html%3Fappc%3D"+appAccount+"%26flag=binding&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirectWxApiClient";
			String unbindurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri=http%3A%2F%2F"+ihost+"%2FphoneWeb%2Fbinding_index.html%3Fappc%3D"+appAccount+"%26flag=unbind&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirectWxApiClient";
			WeixinMenu weiguanwang = new WeixinMenu();
			weiguanwang.setGid(0);weiguanwang.setMtype("view");weiguanwang.setName("微官网");weiguanwang.setInputCode("");weiguanwang.setUrl("");weiguanwang.setParentId(0);weiguanwang.setId(60016);weiguanwang.setStrfalg("aaaaaa");
			list.add(weiguanwang);
			WeixinMenu guanwang = new WeixinMenu();
			guanwang.setGid(0);guanwang.setMtype("view");guanwang.setName("微官网");guanwang.setInputCode("");guanwang.setUrl(guanwangurl);guanwang.setParentId(60016);guanwang.setId(60017);guanwang.setStrfalg("bbbbbb");
			list.add(guanwang);
			WeixinMenu binding = new WeixinMenu();
			binding.setGid(0);binding.setMtype("view");binding.setName("绑定");binding.setInputCode("");binding.setUrl(bindingurl);binding.setParentId(60016);binding.setId(60018);binding.setStrfalg("cccccc");
			list.add(binding);
			WeixinMenu unbind = new WeixinMenu();
			unbind.setGid(0);unbind.setMtype("view");unbind.setName("解绑");unbind.setInputCode("");unbind.setUrl(unbindurl);unbind.setParentId(60016);unbind.setId(60019);unbind.setStrfalg("dddddd");
			list.add(unbind);
			session.setAttribute("weixin_menu_group", list);
		}else{
			list = (List<WeixinMenu>) session.getAttribute("weixin_menu_group");
		}
		List<WeixinMenu> copylist = CopyList.deepCopy(list);
		return weixinMenuBo.makeTreeData(copylist);
	}
	
	/**
	 * DB删除菜单组
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteByIds")
	@ResponseBody
	public Object deleteMenuGroup(int[] ids) {
		return weixinMenuBo.deleteMenuGroup(ids);
	}
	
	@RequestMapping("/deleteMenu")
	@ResponseBody
	public Object deleteMenu(int id ) {
		return weixinMenuBo.deleteMenu(id);
	}
	
	@RequestMapping("/deleteMenuByS")
	@ResponseBody
	public Object deleteMenuByS(HttpSession session,HttpServletRequest request, int id ,String strfalg ) {
		if(session.getAttribute("weixin_menu_group")==null || strfalg == null || "".equals(strfalg)){
			return 0;
		}else{
			List<WeixinMenu> list = (List<WeixinMenu>) session.getAttribute("weixin_menu_group");
			int i = list.size();
			list = weixinMenuBo.deleteMenuByS(list,strfalg);
			return i - list.size();
		}
	}
	
	/**
	 * 新增菜单组
	 * @param weixinMenuGroup
	 * @return
	 */
	@RequestMapping("/addMenuGroup")
	@ResponseBody
	public Object addMenuGroup(HttpSession session,HttpServletRequest request,WeixinMenuGroup weixinMenuGroup) {
		List<WeixinMenu> list = (List<WeixinMenu>) session.getAttribute("weixin_menu_group");
		if(list != null && list.size() > 0){
			UserInfo userInfo = (UserInfo) session
					.getAttribute(GlobalConstant.USER_INFO);
			weixinMenuGroup.setSchoolId(userInfo.getSchoolId());
			if(weixinMenuBo.addMenuGroup(weixinMenuGroup) > 0){
				if(weixinMenuBo.addMenuByA(list , weixinMenuGroup) > 0){
					session.removeAttribute("weixin_menu_group");
					return "ok";
				}else{
					return "uniqueValue_菜单组";
				}
			}else{
				return "uniqueValue_菜单组";
			}
		}else{
			return "uniqueValue_菜单组";
		}
	}
	
	/**
	 * 插入session菜单
	 * @param session
	 * @param request
	 * @param weixinMenu
	 * @return
	 */
	@RequestMapping("/addMenuByS")
	@ResponseBody
	public Object addMenuByS(HttpSession session,HttpServletRequest request,WeixinMenu weixinMenu) {
		List<WeixinMenu> list = new ArrayList<WeixinMenu>();
		if(session.getAttribute("weixin_menu_group")!=null){
			list = (List<WeixinMenu>) session.getAttribute("weixin_menu_group");
			if(list == null){
				list = new ArrayList<WeixinMenu>();
			}
			list.add(weixinMenu);
			session.setAttribute("weixin_menu_group", list);
		}else{
			list.add(weixinMenu);
			session.setAttribute("weixin_menu_group", list);
		}
		return list.size();
	}
	
	/**
	 * DB更新中新增菜单
	 * @param weixinMenu
	 * @return
	 */
	@RequestMapping("/addMenuByU")
	@ResponseBody
	public Object addMenuByU(WeixinMenu weixinMenu) {
		return weixinMenuBo.addMenuByU(weixinMenu);
	}
	
	/**
	 * DB更新中修改菜单
	 * @param weixinMenu
	 * @return
	 */
	@RequestMapping("/updateMenuByU")
	@ResponseBody
	public Object updateMenuByU(WeixinMenu weixinMenu) {
		return weixinMenuBo.updateMenuByU(weixinMenu);
	}
	
	/**
	 * DB更新菜单组
	 * @param weixinMenuGroup
	 * @return
	 */
	@RequestMapping("/updateGroupByUId")
	@ResponseBody
	public Object updateGroupByUId(WeixinMenuGroup weixinMenuGroup) {
		if( weixinMenuGroup.getName() == null || weixinMenuGroup.getId() == null ){
			return "uniqueValue_菜单组";
		}
		if(weixinMenuBo.updateGroupByUId(weixinMenuGroup)>0){
			return "ok";
		}else{
			return "uniqueValue_菜单组";
		}
	}
	
	
	@RequestMapping("/updateMenuByS")
	@ResponseBody
	public Object updateMenuByS(HttpSession session,HttpServletRequest request,WeixinMenu weixinMenu) {
		if(session.getAttribute("weixin_menu_group")==null || weixinMenu.getStrfalg() == null){
			return 0;
		}
		List<WeixinMenu> list = (List<WeixinMenu>) session.getAttribute("weixin_menu_group");
		int j = 0 ;
		for(int i = 0 ; i < list.size() ; i ++){
			WeixinMenu wxm = list.get(i);
			if( weixinMenu.getStrfalg().equals( wxm.getStrfalg() ) ){
				wxm.setUrl(weixinMenu.getUrl());
				wxm.setInputCode(weixinMenu.getInputCode());
				wxm.setMtype(weixinMenu.getMtype());
				wxm.setName(weixinMenu.getName());
				j++;
			}
		}
		return j;
	}
	
}
